package com.hk.Mgr.web.controller.remoteCall;

import com.hk.Mgr.web.dto.AppVersionDTO;
import com.hk.base.BaseCtrl;
import com.hk.commons.entity.sys.SysUser;
import com.hk.commons.vo.JsonResult;
import com.hk.service.sys.ISysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

/**
 * Created by Antinomy on 17/1/22.
 */

@Controller
@RequestMapping(value = BaseCtrl.App + "/version")
public class AppVersionController {
    @Autowired
    private ISysUser sysUserService;

    @Value("${activity.baseUrl}")
    private String baseUrl;

    private RestTemplate template = new RestTemplate();

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult list() {
        String url = baseUrl + "api/app/versions";

        JsonResult result = template.getForObject(url, JsonResult.class);
        return result;
    }

    @RequestMapping(value = "/history", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult history(String channel,String phoneType) {
        String url = baseUrl + "api/app/versions/history/"+channel+"/"+phoneType;

        JsonResult result = template.getForObject(url, JsonResult.class);
        return result;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult update(AppVersionDTO dto) {

        String url = baseUrl + "api/app/version/"
                + dto.getChannel().toString()
                + "/" + dto.getPhoneType().toString();

        Long userId = dto.getUserId();
        SysUser user = sysUserService.findById(userId);
        dto.setOperator(user.getRealName());

        JsonResult result = new JsonResult();
        try {
            template.put(url, dto);
            result.resultCode = "200";
        } catch (Exception ex) {
            result.resultCode = "500";
            result.msg = ex.getMessage();
        }

        return result;
    }
}
